#pragma once
#include "stdafx.h"
#include "TimeAnalyCmd.h"
#include "TimeAnalysis.h"
#include "BasicInfo.h"
#include "DT56Dlg.h"
#include "DTData.h"

CTimeAnalyCmd::CTimeAnalyCmd()
{
}

CTimeAnalyCmd::CTimeAnalyCmd(SLITER_SEL Sel, CString strOld, CString strNew, int nPile, int nHam, MODIFY_RANGE range, WAVE_OPERATE Operate)
{
	m_Sel = Sel;
	m_strOld = strOld;
	m_strNew = strNew;
	m_Range = range;
	m_nPile = nPile;
	m_nHam = nHam;
	m_Operate = Operate;
	CDT56Dlg::m_pDTWnd->SetMenuUndo(TRUE);
	GetValue(m_Range, m_Sel, m_vecVal);
}

CTimeAnalyCmd::CTimeAnalyCmd(SLITER_SEL Sel, CString strOld[2], CString strNew[2], int nPile, int nHam, MODIFY_RANGE range, WAVE_OPERATE Operate)
{
	m_Sel = Sel;
	m_strOld = strOld[0];
	m_strOld2 = strOld[1];
	m_strNew = strNew[0];
	m_strNew2 = strNew[1];
	m_Range = range;
	m_nPile = nPile;
	m_nHam = nHam;
	m_Operate = Operate;
	CDT56Dlg::m_pDTWnd->SetMenuUndo(TRUE);
	if (m_Sel == EXP_POS)
	{
		GetValue(m_Range, ENLARGE_EXP, m_vecVal);
		GetValue(m_Range, ENLARGE_POS, m_vecVal2);
	}
	else if (m_Sel == HP_LP)
	{
		GetValue(m_Range, LP_VAL, m_vecVal);
		GetValue(m_Range, HP_VAL, m_vecVal2);
	}
}


CTimeAnalyCmd::~CTimeAnalyCmd()
{
	
}

void CTimeAnalyCmd::Redo()
{
	ResetNewValue(m_Sel, m_strNew, m_nPile, m_nHam, m_Range, m_strNew2);
}
void CTimeAnalyCmd::Undo()
{
	ResetOldValue(m_Sel, m_strOld, m_nPile, m_nHam, m_Range, m_strOld2);
}

void CTimeAnalyCmd::ResetNewValue(SLITER_SEL Sel, CString strVal, int nPile, int nHam, MODIFY_RANGE Range, CString strVal1)
{
	if (m_Range == RANGE_HAM)
	{
		CDTData::GetInstance()->ModifyTimeAnalyValue(Range, Sel, strVal, strVal1, nPile, nHam, m_Operate);
	}
	else if (m_Range == RANGE_PILE)
	{
		CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, strVal, strVal1, nPile, AVG_WAVE_NO, m_Operate);
		for (int i = 0; i < (int)CDTData::GetInstance()->m_vvecHam[m_nPile].size(); ++i)
		{
			CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, strVal, strVal1, nPile, i);
		}
	}
	else
	{
		int nCnt = 0;
		for (int k = 0; k < (int)CDTData::GetInstance()->m_vvecHam.size(); ++k)
		{
			CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, strVal, strVal1, k, AVG_WAVE_NO, m_Operate);
			nCnt++;
			for (int i = 0; i < (int)CDTData::GetInstance()->m_vvecHam[k].size(); ++i)
			{
				CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, strVal, strVal1, nPile, i, m_Operate);
				nCnt++;
			}
		}
	}

	if (Sel == WAVE_SPEED || Sel == PILE_LEM)
	{
		CLeftDlg::m_pLeftWnd->m_BasicInfoDlg->SetFoucsByUndo(Sel, strVal, nPile, nHam);
	}
	else
	{
		if (Sel == HP_LP)
		{
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(LP_VAL, strVal, nPile, nHam);
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(HP_VAL, strVal1, nPile, nHam);
		}
		else if (Sel == EXP_POS)
		{
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(ENLARGE_EXP, strVal, nPile, nHam);
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(ENLARGE_POS, strVal, nPile, nHam);
		}
		else
		{
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(Sel, strVal, nPile, nHam);
		}
	}
}

void CTimeAnalyCmd::ResetOldValue(SLITER_SEL Sel, CString strVal, int nPile, int nHam, MODIFY_RANGE Range, CString strVal1)
{
	
	if (m_Range == RANGE_HAM)
	{
		if (Sel == HP_LP || Sel == EXP_POS)
		{
			CDTData::GetInstance()->ModifyTimeAnalyValue(Range, Sel, m_vecVal[0], m_vecVal2[0], nPile, nHam, m_Operate);
		}
		else
		{
			CDTData::GetInstance()->ModifyTimeAnalyValue(Range, Sel, m_vecVal[0], L"", nPile, nHam, m_Operate);
		}
	}
	else if (m_Range == RANGE_PILE)
	{
		if (Sel == HP_LP || Sel == EXP_POS)
		{
			CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[0], m_vecVal2[0], nPile, AVG_WAVE_NO, m_Operate);
			for (int i = 0; i < (int)CDTData::GetInstance()->m_vvecHam[m_nPile].size(); ++i)
			{
				CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[i + 1], m_vecVal2[i + 1], nPile, i);
			}
		}
		else
		{
			CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[0], L"", nPile, AVG_WAVE_NO, m_Operate);
			for (int i = 0; i < (int)CDTData::GetInstance()->m_vvecHam[m_nPile].size(); ++i)
			{
				CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[i + 1], L"", nPile, i);
			}
		}
		
	}
	else
	{
		int nCnt = 0;
		if (Sel == HP_LP || Sel == EXP_POS)
		{
			for (int k = 0; k < (int)CDTData::GetInstance()->m_vvecHam.size(); ++k)
			{
				CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[nCnt], m_vecVal2[nCnt], k, AVG_WAVE_NO, m_Operate);
				nCnt++;
				for (int i = 0; i < (int)CDTData::GetInstance()->m_vvecHam[k].size(); ++i)
				{
					CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[nCnt], m_vecVal2[nCnt], nPile, i, m_Operate);
					nCnt++;
				}
			}
		}
		else
		{
			for (int k = 0; k < (int)CDTData::GetInstance()->m_vvecHam.size(); ++k)
			{
				CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[nCnt], L"", k, AVG_WAVE_NO, m_Operate);
				nCnt++;
				for (int i = 0; i < (int)CDTData::GetInstance()->m_vvecHam[k].size(); ++i)
				{
					CDTData::GetInstance()->ModifyTimeAnalyValue(RANGE_HAM, Sel, m_vecVal[nCnt], L"", nPile, i, m_Operate);
					nCnt++;
				}
			}
		}	
	}

	if (Sel == WAVE_SPEED || Sel == PILE_LEM)
	{
		CLeftDlg::m_pLeftWnd->m_BasicInfoDlg->SetFoucsByUndo(Sel, strVal, nPile, nHam);
	}
	else
	{
		if (Sel == HP_LP)
		{
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(LP_VAL, strVal, nPile, nHam);
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(HP_VAL, strVal1, nPile, nHam);
		}
		else if (Sel == EXP_POS)
		{
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(ENLARGE_EXP, strVal, nPile, nHam);
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(ENLARGE_POS, strVal, nPile, nHam);
		}
		else
		{
			CTimeAnalysis::m_pTimeALyWnd->SetFoucsByUndo(Sel, strVal, nPile, nHam);
		}	
	}
}

void CTimeAnalyCmd::GetValue(MODIFY_RANGE range, SLITER_SEL Sel, vector<CString> &vecVal)
{
	CDTData::GetInstance()->GetOperateValue(range, Sel, vecVal);
}

